package br.eng.eliseu.gwt.aconchego.camera.server.command;

import java.io.Serializable;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmailCommand implements Serializable {
	
	private static final long serialVersionUID = -3503312538232518320L;
	
	private String recipient;
	private String recipientCC;
	private String recipientCCO;
	private String subject;
	private String text;
	
	public void call()  {
		try{
			Properties props = new Properties();
			props.put("mail.transport.protocol", "smtps");
			props.put("mail.smtps.host", "smtp.gmail.com");
			props.put("mail.smtps.auth", "true");
			final Session mailSession = Session.getDefaultInstance(props);
			mailSession.setDebug(false);

			final MimeMessage message = new MimeMessage(mailSession);
			message.setSubject(subject, "UTF-8");
			message.setText(text, "UTF-8");
			message.setHeader("Content-Type", "text/html; charset=UTF-8");
			
			message.setFrom(new InternetAddress("administrador@aconchegohotelescola.com.br", "Aconchego"));
			if(recipient!=null){
				message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(recipient));
			}
			if(recipientCCO!=null){
				message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(recipientCCO));
			}
			if(recipientCC!=null){
				message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(recipientCC));
			}

			
			Thread t = new Thread(new Runnable() {
				public void run() {
					try {
						Transport transport = mailSession.getTransport();
						transport.connect("smtp.gmail.com", 465, "administrador@aconchegohotelescola.com.br","eliseurm");
						
						transport.sendMessage(message, message.getAllRecipients());
						transport.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			t.start();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	public String getRecipient() {
		return recipient;
	}
	public void setRecipient(String recipient) {
		this.recipient = recipient;
	}
	public String getRecipientCC() {
		return recipientCC;
	}
	public void setRecipientCC(String recipientCC) {
		this.recipientCC = recipientCC;
	}
	public String getRecipientCCO() {
		return recipientCCO;
	}
	public void setRecipientCCO(String recipientCCO) {
		this.recipientCCO = recipientCCO;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}